<!-- 研究者费用申请列表 -->
<template>
  <div>
    <s-table
      ref="table"
      size="default"
      :rowKey="record => record.id"
      bordered
      :columns="$auth('PROJECT.PROJECT_FEE_RESEARCHER_LIST') ? columns:[]"
      :data="loadData">
      <template slot="title">
        <a-descriptions title="研究者费用申请"></a-descriptions>
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="申请状态">
                  <a-select v-model="queryParam.status" placeholder="请选择" default-value="0" allowClear>
                    <a-select-option v-for="item in APPLY_QUERY_STATUS" :key="item.value" :value="item.value">{{
                      item.desc
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="研究者编号">
                  <a-input v-model="queryParam.researchDetailNo" placeholder="请输入" />
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="12" :sm="24">
                  <a-form-model-item label="选择时间:" labelAlign="left">
                    <a-range-picker @change="handleSelectDate" v-model="queryDate" allowClear :show-time="true" format="YYYY-MM-DD HH:mm:ss" />
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :md="(!advanced && 8) || 24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
                >
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px;" @click="reset">重置</a-button>

                  <a @click="toggleAdvanced" style="margin-left: 8px;">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form>
          <div>
            <a-button v-if="$auth('PROJECT.PROJECT_FEE_RESEARCHER_COMMIT')" @click="addApply" type="primary" icon="plus" style="margin-left: 8px;">
              新增申请
            </a-button>
          </div>
        </div>
      </template>
      <template slot="id" slot-scope="text, record, index">
        {{ index | getPageIndex($refs.table) }}
      </template>
      <template slot="createTime" slot-scope="text, record">
        {{ record.createTime | moment() }}
      </template>
      <template slot="status" slot-scope="text, record">
        {{ record.status | enumValueByKey($const.EXAMINE_TYPE) }}
      </template>
      <template slot="operation" slot-scope="text, record">
        <a v-if="$auth('PROJECT_FEE.PROJECT_FEE_RESEARCHER_DETAIL')" @click="clickView(record)">查看</a>
      </template>
    </s-table>
  </div>
</template>
<script>
import { APPLY_QUERY_STATUS } from '@/constants/common'
import monitor from '@/utils/monitor'
import browseResearch from '@/monitor/views/cost-apply/research/research'
import { buildPaginationObj } from '@/components/Table'
import { getCostList } from '@/api/cost/cost'
const watch = monitor(browseResearch)
export default {
  props: {
    proId: {
      type: Number,
      default: null
    },
    approval: Object
  },
  components: {},
  data () {
    return {
      APPLY_QUERY_STATUS,
      queryParam: {
        type: 1,
        proId: this.proId
      },
      queryDate: [],
      loadData: parameter => {
        if (this.$auth('PROJECT.PROJECT_FEE_RESEARCHER_LIST')) {
          if (this.queryParam.proId) {
            return getCostList(Object.assign(parameter, this.queryParam))
              .then(res => {
                return res
              })
              .catch(() => {})
            }
        } else {
          return Promise.resolve(buildPaginationObj([]))
        }
      },
      advanced: false,
      columns: [
        {
          title: '序号',
          key: 'id',
          dataIndex: 'id',
          scopedSlots: { customRender: 'id' }
        },
        {
          title: '研究者编号',
          key: 'researchDetailNo',
          dataIndex: 'researchDetailNo'
        },
        {
          title: '申请备注',
          key: 'applyReason',
          dataIndex: 'applyReason'
        },
        {
          title: '申请金额',
          key: 'totalAmount',
          dataIndex: 'totalAmount'
        },
        {
          title: '申请时间',
          key: 'createTime',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '当前状态',
          key: 'status',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  mounted () {
    this.browsePage()
    this.$globalVue.$on('approvalCostUpdate', () => this.$refs.table.loadData())
  },
  destroyed () {
    this.$globalVue.$off('approvalCostUpdate')
  },
  methods: watch({
    // 点击展开 详细搜索信息
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 点击重置
    reset () {
      this.queryParam = { type: 1, proId: this.proId }
      this.queryDate = []
      this.$refs.table.refresh(true)
    },
    // 点击新增
    addApply () {
      if (!this.approval) return this.$message.warning('当前审批流程还没配置，暂不能发起费用申请')
      this.$router.push({
        name: 'AddResearcher',
        query: {
          pname: this.$store.state.user.project.name,
          id: this.proId
        }
      })
    },
    // 点击查看
    clickView (record) {
      this.$router.push({
        name: 'ApplyDetail',
        query: {
          id: record.id,
          status: record.status
        }
      })
    },
    // 处理发起日期选择框改变时产生的回调
    handleSelectDate (date, dateString) {
      this.queryDate = date
      this.queryParam.applyStartTime = dateString[0]
      this.queryParam.applyEndTime = dateString[1]
      console.log(date, dateString, '打印Date')
    },
    // 研究者费用申请页面浏览埋点
    browsePage () {}
  })
}
</script>
